









************************************************
*HISTORICAL VERSION
*
*
*Set list of margins; becomes points on x axis
************************************************

cd $directory

global MARGLIST = "50 75 100 150 225 330 500 750 1000 1500 2250 3300 5000 7500 10000 15000 22500 33000 50000 75000 100000"
global MARGLISTPCT = ".0000001 .0000002 .0000005 .000001 .000002 .000005 .00001 .00002 .00005 .0001 .0002 .0005 .001"

global LIST = "52201 52204 52210 52211 22202 12201" //M1; M12 (1964-2016); 52210 is 1936-1956; 52211 is 1916-1932;  R1; A1


************************************************
*Outer loop - open datasets
************************************************

global LOOP = 2

foreach file in $LIST{

	use "source_data`c(dirsep)'`file'.dta", clear

	
************************************************
*Make vars
************************************************	

	if `file'==52201{
		local simlabel = "1988-2016"
		local range = 538
		local xline = 270
		local textloc = 5
		local party = "Republican"
	}
	if `file'==52204{
		local simlabel = "1964-2016"
		local range = 538
		local xline = 270
		local textloc = 5
		local party = "Republican"
	}
	if `file'==52210{
		local simlabel = "1936-1956"
		local range = 531
		local xline = 266
		local textloc = 5
		local party = "Republican"
	}
	if `file'==52211{
		local simlabel = "1916-1932"
		local range = 531
		local xline = 266
		local textloc = 5
		local party = "Republican"
	}
	if `file'==22202{
		local simlabel = "1872-1888"
		local range = 401
		local xline = 201
		local textloc = 5
		local party = "Republican"
	}
	if `file'==12201{
		local simlabel = "1836-1852"
		local range = 288
		local xline = 145
		local textloc = 5
		local party = "Whig"
	}
		
	//count states 
	local error=0
	local counter=1
	while `error' != 111 {
		qui cap d total_ev_`counter'
		local error=_rc
		local counter=`counter'+1
	}
	global STATECOUNT = `counter'-2 
	di "$STATECOUNT"
	local state_count = `counter'-2 

	//make vars that summarize each election
	qui egen pvotes = rowtotal(twoPartyVotes_*)
	forvalues i = 1(1)`state_count'{
		qui gen rvotes_`i' = twoPartyVotes_`i'* R_pvShare_`i'
	}
	qui egen rvotes = rowtotal(rvotes_*)
	qui gen pvshare = rvotes/pvotes
	qui gen margin_share = abs(0.5-pvshare)*2
	qui gen margin_votes = margin_share*pvotes

	forvalues i = 1(1)`state_count'{
		qui gen rwin_`i' = R_pvShare_`i' > .5
		qui gen rev_`i' = rwin_`i' * total_ev_`i'
	}
	qui egen revn_total= rowtotal(rev_*)
	qui egen ev_denominator = rowtotal(total_ev_*)
	qui gen rwin = revn_total/ev_denominator > 0.5
	qui gen dwin = (ev_denominator-revn_total)/ev_denominator > 0.5
	qui gen cf_revn_total  = revn_total
	qui gen cf_rwin = rwin

	qui gen inv=0
	qui replace inv=1 if  (rwin==1 & pvshare<.5) | (rwin==0 & pvshare>.5)

	
	local labsize = "medium"
	hist revn_total  , ///
		xtitle("`party' EC Votes", size(`labsize'))  ///
		bfcolor(gs8) blcolor(gs8) ///	
		graphregion(color(white)) plotregion(color(white)) ///
		xsc( r(0 `range')) ///
		start(0) fysize(25) width(5) barwidth(3.5) ///
		xlabel(0 `xline' `range', nogrid labsize(`labsize')) ///
		ylabel(none) xline(`xline', lcolor(gs11) lpattern(dash) ) ///		
		ytitle("") ///
		text(.0075 `textloc' "`simlabel'" , color(gs8) place(e) justification(left) size(`labsize')) ///
		name(hist_`file', replace) saving(figures`c(dirsep)'hist_`file', replace)	

		*graph export "figures`c(dirsep)'Main_modern.pdf", replace	
	
************************************************
*Outer loop
************************************************
	
global LOOP = $LOOP +1		
di "$LOOP"
	
}
global ENDLOOP = $LOOP

************************************************
*Figures
************************************************


graph combine "figures`c(dirsep)'hist_52201.gph" "figures`c(dirsep)'hist_52204.gph" "figures`c(dirsep)'hist_52210.gph" "figures`c(dirsep)'hist_52211.gph" "figures`c(dirsep)'hist_22202.gph" "figures`c(dirsep)'hist_12201.gph", ///
		graphregion(color(white)) rows(2) cols(3) xsize(5) ysize(3) xcommon ycommon name(combine2, replace)
		
graph export "figures`c(dirsep)'FigureA3b.pdf", replace

